/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itttq.gshop.domain.po.ErpOrderDetailPo;
import com.itttq.gshop.domain.po.ErpOrderPo;
import com.itttq.gshop.domain.po.WareInfoPo;
import com.itttq.gshop.domain.to.ErpOrderTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.domain.to.ResultTo;
import com.itttq.gshop.generic.util.Constants;
import com.itttq.gshop.pbpm.mapper.IErpOrderDetailMapper;
import com.itttq.gshop.pbpm.mapper.IErpOrderMapper;
import com.itttq.gshop.pbpm.mapper.IWareInfoMapper;
@Service
public class ErpOrderService {
	@Autowired
	private IErpOrderMapper orderMapper;
	@Autowired
	private IErpOrderDetailMapper orderDetailMapper;
	@Autowired
	private IWareInfoMapper wareInfoMapper;
	
	/**
	 * 根据ID获取ErpOrderPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return ErpOrderPo
	 */
	public ErpOrderPo getErpOrder(int id) {
		return orderMapper.findErpOrderById(id);
	}
	
	/**
	 * 根据条件获取ErpOrderPo对象，并返回查询的数据总数
	 * 
	 * @param order
	 * @Author 迷蝶
	 * @return ResultPageTo<ErpOrderPo>
	 */
	public ResultPageTo<ErpOrderPo> getErpOrders(ErpOrderPo order) {
		return new ResultPageTo<ErpOrderPo>(order.getStart(), order.getSize(), 
				orderMapper.findErpOrderCount(order), orderMapper.findErpOrders(order));
	}
	
	/**
	 * 根据条件获取订单和商品明细对象，不返回查询的数据总数
	 * 
	 * @param order
	 * @Author 迷蝶
	 * @return ResultPageTo<ErpOrderPo>
	 */
	public List<ErpOrderTo> getOrders(ErpOrderPo order) {

		List<ErpOrderPo> orders = orderMapper.findErpOrders(order);
		if(null == orders || orders.isEmpty()) {
			return null;
		}
		List<Integer> orderIds = new ArrayList<Integer>();
		Map<Integer, ErpOrderTo> orderMap = new HashMap<Integer, ErpOrderTo>();
		for(ErpOrderPo op : orders) {
			orderIds.add(op.getId());
			ErpOrderTo ot = new ErpOrderTo();
			BeanUtils.copyProperties(op, ot);
			orderMap.put(op.getId(), ot);
		}
		// 获取所有订单的订单商品数据
		List<ErpOrderDetailPo> orderDetails = orderDetailMapper.findOrderDetailByOrderIds(orderIds);
		for(ErpOrderDetailPo odp : orderDetails) {
			ErpOrderTo ot = orderMap.get(odp.getErpOrderId());
			List<ErpOrderDetailPo> odps = ot.getOrderDetails();
			if(null == odps) {
				odps = new ArrayList<ErpOrderDetailPo>();
			}
			odps.add(odp);
			ot.setOrderDetails(odps);
			orderMap.put(odp.getErpOrderId(), ot);
		}
		// 设置到orderTo的集合中
		List<ErpOrderTo> orderTos = new ArrayList<ErpOrderTo>();
		for(ErpOrderPo oip : orders) {
			orderTos.add(orderMap.get(oip.getId()));
		}
		return orderTos;
	}
	
	/**
	 * 根据ID删除ErpOrderPo对象
	 * 
	 * @param id
	 * @Author 迷蝶
	 */
	public Integer delErpOrder(int id) {
		return orderMapper.delErpOrderById(id);
	}
	
	/**
	 * 增加ErpOrderPo对象
	 * 
	 * @param order
	 * @Author 迷蝶
	 */
	@Transactional
	public ResultTo addErpOrder(ErpOrderTo order) {
		ResultTo rt = new ResultTo();

		List<WareInfoPo> wares = new ArrayList<WareInfoPo>();
		// 判断库存
		for(ErpOrderDetailPo odp : order.getOrderDetails()) {
			// 借用STATUTS_OPEN的值，表达的是商品类型。
			if(odp.getType().intValue() == Constants.STATUTS_OPEN) {
				WareInfoPo ware = new WareInfoPo();
				ware.setId(odp.getWareId());
				ware.setNumberInventory(odp.getNumber());
				wares.add(ware);
			}
		}
		if(null != wares && !wares.isEmpty()) {
			// TODO 删除和扣减失败时的处理
			// 扣减库存
			Map<String, List<WareInfoPo>> waresMap = new HashMap<String, List<WareInfoPo>>();
			waresMap.put(Constants.OP_INVENTORY_ADD, wares);
			wareInfoMapper.updateInventoryBatch(waresMap);
		}
		// 生成订单
		int rs = orderMapper.addErpOrder(order);
		if(rs != ResultTo.SUCCESS) {
			rt.setCode(ResultTo.FAIL);
			return rt;
		}
		// 增加到订单商品记录
		for(ErpOrderDetailPo odp : order.getOrderDetails()) {
			odp.setErpOrderId(order.getId());
		}
		Map<String, List<ErpOrderDetailPo>> map = new HashMap<String, List<ErpOrderDetailPo>>();
		map.put("details", order.getOrderDetails());
		orderDetailMapper.insertBatch(map);
		// 如果成功，则msg记录orderId返回
		if(rs == ResultTo.SUCCESS) {
			rt.setMsg(String.valueOf(order.getId()));
			return rt;
		}
		return rt;
	}
	
	/**
	 * 根据条件更新ErpOrderPo对象
	 * 
	 * @param order
	 * @Author 迷蝶
	 */
	public Integer updateErpOrder(ErpOrderPo order) {
		return orderMapper.updateErpOrder(order);
	}
	
}
